﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace batchMail
{
    /// <summary>
    /// 发送结果
    /// </summary>
    public class SendResult
    {

        /// <summary>
        /// 发送文件的路径
        /// </summary>
        public string FilePath { get; set; }


        /// <summary>
        /// 源Email
        /// </summary>
        public string FromEmail { get; set; }


        /// <summary>
        /// 目的email
        /// </summary>
        public string ToEmail { get; set; }


        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; set; }


        /// <summary>
        /// 失败的原因
        /// </summary>
        public string message { get; set; }


        /// <summary>
        /// 序列化为字符串
        /// </summary>
        /// <returns>序列化的字符串 </returns>
        public string ToLogString()
        {
            return $"{FilePath}\t{FromEmail}\t{ToEmail}\t{(IsSuccess?1:0)}\t{message}";
        }


        /// <summary>
        /// 保存日志文件
        /// </summary>
        /// <param name="path"></param>
        public static void SaveLogFile(SendResult[] sendList)
        {
            string path = $".\\{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.txt";

            FileStream fStream = new FileStream(path, FileMode.Create);

            StreamWriter writer = new StreamWriter(fStream);

            foreach (var send in sendList)
            {
                writer.WriteLine(send.ToLogString());
            }

            writer.Flush();
            writer.Close();
            fStream.Close();

            writer.Dispose();
            fStream.Dispose();

            writer = null;
            fStream = null;

               
        }



    }
}
